import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_eve_plugin/components/ye_xiu/checkpoint/checkpoint_start_game_center.dart';
import 'package:flutter_eve_plugin/components/ye_xiu/checkpoint/checkpoint_start_game_footer.dart';
import 'package:flutter_eve_plugin/constants/font_family.dart';
import 'package:flutter_eve_plugin/constants/font_weight.dart';
import 'package:flutter_eve_plugin/constants/hero_tag.dart';
import 'package:flutter_eve_plugin/model/ye_xiu/checkpoint/user_card.dart';
import 'package:flutter_eve_plugin/pages/ye_xiu/checkpoint/actions.dart';
import 'package:flutter_eve_plugin/store/main.dart';
import 'package:flutter_eve_plugin/store_vm/app_vm.dart';
import 'package:flutter_eve_plugin/utils/image_util.dart';
import 'package:flutter_eve_plugin/utils/screen_util.dart';
import 'package:flutter_redux/flutter_redux.dart';

class CheckpointStartGamePage extends StatefulWidget {
  final String title;

  CheckpointStartGamePage({Key key, this.title}) : super(key: key);

  _CheckpointStartGamePageState createState() =>
      _CheckpointStartGamePageState();
}

class _CheckpointStartGamePageState extends State<CheckpointStartGamePage> {
  @override
  Widget build(BuildContext context) {
    return StoreConnector<AppState, AppStateViewModel>(onInit: (store) {
      ///更新单个副本列表
      store.dispatch(
          stages(store.state.yeXiuState.checkpointState.dungeonid, context));

      ///更新副本列表
      store.dispatch(dungeonList(1));

      ///更新活力值
      store.dispatch(vitality(context));
    }, converter: (store) {
      return AppStateViewModel(state: store.state);
    }, builder: (context, vm) {
      var stages = vm.state.yeXiuState.checkpointState.checkpointStages;
      var stageInfo = vm.state.yeXiuState.checkpointState.checkpointStageInfo;
      var userCard = vm.state.yeXiuState.checkpointState.userCard;
      var selectMap = vm.state.yeXiuState.checkpointState.selectMap;
      List<CardList> showListTmp = [];
      userCard?.cards?.forEach((child) {
        if (child?.card?.name != '君莫笑') {
          showListTmp.add(child);
        }
      });
      List<CardList> showList = [];
      selectMap?.forEach((k, v) {
        showListTmp?.forEach((item) {
          if (v == item?.card?.cardId) {
            showList.add(item);
          }
        });
      });
      return WillPopScope(
        onWillPop: () => Future.value(false),
        child: Container(
          decoration: BoxDecoration(
              image: DecorationImage(
                  image: AssetImage(
                      imageUtilYeXiu('checkpoint/icon_checkpoint_bg@3x')),
                  fit: BoxFit.fitWidth,
                  alignment: Alignment.topCenter)),
          child: SafeArea(
            child: SingleChildScrollView(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Container(
                    margin: EdgeInsets.only(top: setRatio(16)),
                    width: setRatio(163),
                    height: setRatio(38),
                    decoration: BoxDecoration(
                        image: DecorationImage(
                            image: AssetImage(imageUtilYeXiu(
                                'checkpoint/icon_checkpoint_start_game_title@3x')))),
                    child: Center(
                      child: Text(
                        stages?.dungeonName ?? '',
                        style: TextStyle(
                            decoration: TextDecoration.none,
                            color: Color(0xFFFFFFFF),
                            fontFamily: FontFamily.PINGFANG,
                            fontWeight: FontWeightCustom.PF_Medium,
                            fontSize: setTextSp(15)),
                      ),
                    ),
                  ),
                  SizedBox(
                    height: setRatio(76),
                  ),
                  Hero(
                    tag: HeroTag.GAME_BOSS_NAME,
                    child: Text(
                      stageInfo?.stageBasics?.configInfo?.bossname ?? '',
                      style: TextStyle(
                          shadows: [
                            BoxShadow(
                                color: Color(0x80000000),
                                offset: Offset(0, setRatio(2)),
                                blurRadius: setRatio(2))
                          ],
                          fontStyle: FontStyle.italic,
                          decoration: TextDecoration.none,
                          color: Color(0xFFFFFFFF),
                          fontFamily: FontFamily.PMZD,
                          fontSize: setTextSp(30)),
                    ),
                  ),
                  SizedBox(
                    height: setRatio(10),
                  ),
                  Hero(
                    tag: HeroTag.GAME_HEAD,
                    child: Image.asset(
                      imageUtilYeXiu(
                          'checkpoint/icon_checkpoint_start_game_vs@3x'),
                      width: setRatio(75),
                      height: setRatio(75),
                    ),
                  ),
                  SizedBox(
                    height: setRatio(20),
                  ),
                  CheckpointStartGameCenterWidget(showList: showList),
                  CheckpointStartGameFooterWidget()
                ],
              ),
            ),
          ),
        ),
      );
    });
  }
}
